<?php
	header('Content-type:text/html;charset=utf-8');
	class ListController extends CommonController{

		public $model;

		public function  __init(){
			parent:: __init();

			$this->model=K('Shop_goods_list');
		}

		public function index(){

			$cid = Q('get.cid',0,'intval');
			// p($cid);die;
			$sonCate = K('Shop_category')->get_choose($cid);
			// p($sonCate);die;
			$sonCid = implode(',', $sonCate);
			// p($sonCid);die;
			//调取货品信息
			$goodsData = K('Shop_goods')->where("Shop_category_cid in ({$sonCid})")->all();
			foreach ($goodsData as $k => $v) {
				$zhe =substr($v['shopprice']/$v['markprice']*10,0,3) ;
				$goodsData[$k]['zhe'] = $zhe;
			}
			// p($goodsData);
			//分配数据
			$this->assign('goodsData',$goodsData);
			#===============================================================

			//调取分类信息
			$cate = K('Shop_category')->where('pid=0')->all();
			$this->assign('cate',$cate);
			#===============================================================

			//调取筛选的品牌数据
			$paiData = K('shop_brand')->all();
			//p($paiData);die;
			$this->assign('paiData',$paiData);

			$bid = Q('get.bid',0,'intval');
			$pinpai = K('Shop_goods')->where("shop_brand_bid={$bid}")->getField('gid',true);
			// p($pinpai);

			//===========================================================
			//调取筛选商品属性信息
			$cids = $this->model->get_son($cid);
			//通过cid获得商品gid
			$gids = K('Shop_goods')->where("shop_category_cid in (" . implode(',',$cids).")")->getField('gid',true);
			//如果有商品的话
			if ($gids) {
				//查到商品属性
				$attrData = K('Shop_goods_attr')->where("shop_goods_gid in (".implode(',',$gids).")")->group('gtvalue')->all();
				// p($attrData);die;
				$tempAttr = array();

				foreach ($attrData as $v) {
					$tempAttr[$v['shop_type_attr_taid']][]=$v;
				}
				$tempAttrFinal = array();
				foreach ($tempAttr as $k => $v) {
					$tempAttrFinal[] = array(
						'tname' => K('Shop_type_attr')->where("taid={$k}")->getField('taname'),
						'tvalue'=> $v,
						);
				}

				//统计属性有多少个
				$total = count($tempAttrFinal);
				//$_GET['param']是这样形式的
				$param = isset($_GET['param'])?explode('_', $_GET['param']): array_fill(0,$total,0);
				// p($param);
				$this->assign('param',$param);
				$temp = array();
				foreach ($param as $v) {
					if ($v) {
						//自关联查出商品
						$temp[] = M()->join('__shop_goods_attr__ g1 JOIN __shop_goods_attr__ g2 ON g1.gtvalue = g2.gtvalue')->where("g1.gtid={$v}")->getField('g2.shop_goods_gid',true);
					}
				}

				// p($temp);
				//取交集
				//array(3,2,5),array(3,2,1),array(3)
				//如果通过属性可以查出商品id
				if ($temp) {
					$intersectTemp = $temp[0];
					foreach ($temp as $k => $v) {
						$intersectTemp = array_intersect($intersectTemp, $v);
					}
				}
				// p($intersectTemp);
				//通过分类查询出来商品gid和我们通过属性查出来gid 再次取交集
				if (isset($intersectTemp)) {
					$finalGid = array_intersect($intersectTemp, $gids, $pinpai);
				}else{
					//点击不限的时候，直接最终结果就是分类查到的商品id
					$finalGid = $gids;
				}
				

			}else{
				//为了页面可以判断是否有商品
				$tempAttrFinal = NULL;
			}
			$this->assign('tempAttrFinal',$tempAttrFinal);
			// p($tempAttrFinal);
			//调出商品数据
			if ($finalGid) {

				$goodDatas = implode(',', $finalGid);
				// p($goodDatas);
				$goodDatas = K("Shop_goods")->where("gid in ({$goodDatas})")->all();

				foreach ($goodDatas as $k => $v) {
					$zhe =substr($v['shopprice']/$v['markprice']*10,0,3) ;
					$goodDatas[$k]['zhe'] = $zhe;
				}
				$this->assign('goodDatas',$goodDatas);
				// p($goodDatas);
			}
			

			$this->dis('list');
		}
		//头部购物车异步调用的方法
		public function gengxin(){

		    $data = K('Shop_cart')->getField('jiesuan',true);
		    foreach ($data as $k => $v) {
		        $tem = unserialize($v);
		    }
		    // p($tem);
		    // p($_SESSION);
		    $this->ajax($tem);
		    
		}




	}












?>